<?php
/**
* DO NOT REQUEST THIS DIRECTLY. ACCESS VIA THE URL:
*		/scribble/inline/js?p=
*
* Where "p" is the ID of the currently loaded Page.
*
* @package Scribble
* @subpackage ScribbleInline
*/
use Buan\UrlCommand;
?>
// Use jquery in noConflict mode and create our our reference to it
jQuery.noConflict();
var SJQ = jQuery;

// Namespace
var ScribbleInline = {
	page: <?php echo json_encode($page->getDbData()); ?>,
	version: <?php echo json_encode($version->getDbData()); ?>,
	pageIsPublished: <?php echo $page->isPublished() ? 'true' : 'false'; ?>,
	outerTemplate: '<?php echo $outerTemplate; ?>',
	innerTemplate: '<?php echo $innerTemplate; ?>'
};

/**
* Draw the control panel to the DOM.
*
* @return void
*/
ScribbleInline.drawControlPanel = function() {

	// Create the control panel and inject into the DOM
	// Start off minimized as otherwise it'll be annoying
	var panelBits = SJQ('<div id="si-control-panel"></div><a href="#" id="si-toggler"><span>Show/Hide Control Panel</span></a>').appendTo(
		SJQ('<div id="si-control-panel-wrapper" class="si-closed"></div>').appendTo(SJQ(document.body))
	);

	// Add elements to main panel area
	var panel = SJQ(panelBits[0]);
	SJQ('<button type="button" onclick="self.location=\'<?php echo htmlentities(UrlCommand::createUrl('scribble', 'page', $page->id), ENT_COMPAT, 'UTF-8'); ?>\';">Edit this page</button>').appendTo(panel);
	SJQ('<p>This page is using the <strong>'+ScribbleInline.outerTemplate+'</strong> outer template, and the <strong>'+ScribbleInline.innerTemplate+'</strong> inner template &mdash; <a href="<?php echo htmlentities(UrlCommand::createUrl('scribble', 'page', $page->id, array('tchange'=>NULL)), ENT_COMPAT, 'UTF-8'); ?>">change now</a></p>').appendTo(panel);

	// Add handlers to toggle button
	var toggler = SJQ(panelBits[1]).click(function() {
		if(SJQ('#si-control-panel-wrapper').hasClass('si-closed')) {
			SJQ('#si-control-panel').slideDown().parent('div').removeClass('si-closed');
		}
		else {
			SJQ('#si-control-panel').slideUp('normal', function() {
				SJQ(this).parent('div').addClass('si-closed');
			});
		}
		this.blur();
		return false;
	});
}

/**
* Initialise the environment.
*
* @return void
*/
ScribbleInline.init = function() {

	// Setup the control panel
	ScribbleInline.drawControlPanel();
}

/**
* When the document is ready, initialise the Scribble inline components.
*/
SJQ(document).ready(ScribbleInline.init);